package Controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"goShopping/superShopping/superShopping/Pay"
	"log"
	"net/http"
	"strconv"
	"time"
)

const (
	KAppID               = "2021000121669356"
	KPrivateKey          = "MIIEpAIBAAKCAQEAnkETnHXk8UTIOkatHUWUgQuo38/M2kwgAOMQIKWkKYYAHmCBNY9GmTaJHUGDzm7VgSdUoqqjuj2j6ben8WTTuCfESizexoAaVEToFy4x0+K6uYtbWhppWFTLcMfsMldHx5mMLRFPtDKfaqPQBvhqzAXsaCk/gY4viSAJBOPVT31iZxk1wM5x68anebe/fZHE0UzNT0pNQk/V64/omHJZ47STT9vkU5FPz9kL81S3FLQ/Os8Wi81/5/EMd6hJbaZC4FWiUe9vHqRV9Jw/9zlFGjsqDi4xsdBNrEK2zxGD9vKKOT1aoxmS05K2sGCzjXzvBeXVhWAwr+jmp4MC/PUo8QIDAQABAoIBAQCPX+gTlWAREj7RRVzrqzFK5LRf6RTwJY3NH+ppmYgsMoIDA22sh5zvjFAXGFcA/LskIuzz3uMtBVFumnxmH/IM4sJcbWB7uG2iMTeQYMK187E2DsJ51HnilBgomogsZ5TZigxMfGWYnVB+veDnn8KqCyfOiqhFiPsaIdLqAjwsobfnHrFdD280AJ8G3+WEct3STar4fNImW3stOiomPC4pua5RC1Qls19gvfEiOTDZ3hacugOtFV1WCVcwNmRwdlZNprwJUIODOFnDu3zobkgsZ4smZdLptB9mv3w9huBEbHa0p1aE80oCbffVWOJWeHrmNskJ0L4cD7kUTOJK48cRAoGBAPoA50PjH3b3f2PVe0RwaMn93PBTywGVXp2lhGt9zIh1dFqx2W6gNR1fasRN0zkhm+z4lIPm5POX5uavSc0GxKYsTkry7tXEjbH8/TcQ0uPgUfIhkbOAcjOyVpXYp4R4+ZGiIv61OjTBvsJUKePg0J4qj0Jxqfq+Z1FiTXuErrpFAoGBAKIMzgw4KbIAGxVWXXrUfK3m/f9e8vNyayG4k4EjVG9xGxXZjvBVsXMVCopoAiUWc0E4W11uJH17wHdO1gsQZFlM5UJ1sRpWl6OBbUSL9rdGFFp5I4I56J5+Qha49esTUXPlhq2bQ3Tld1lsGWPva+QA8Nmwf7v3IROPinb4a1S9AoGBAM/nkmuFZLRFrJ30ehd3N93LDUKbHHbLpiyJ6i6OJlrQ+ps1zfFOr/0mTZ7EbcTUK1yNeMycZoghKfdrjH9BgqYCsHVpw67oT0HWdymlBwxUjzFCM23IgrB1PcZNrznedQVkhZ+2TJp1CNOItHF6k2MFV6/U4JvSXg9zJaZzt+7VAoGAO0qqHWhzQRCu2CjIfUe/8rUg3E42YILN1gG93rM5kqj0uxLmLON90NizCZ8yobVMKGHhdBb3vP4Jlt2+jT/LJ61t+NotGbDzPLz/aOQ5OienDM9kAaMEjPEvZzb3q7JezCQAGgdEMEJBX/OgeqNAe6Uoes4Mfo7WiFyt0gfk5ZUCgYA5T3CQfCCoIS+ABbBAZtAigZdBIIK6eV7gCRzgyZqtZgMjy57foIYTsIJZWCCmrlGcSH8x71FW2emhuH96uRlibpJWX74+fsKvzCo4KO90k1XpU4p1lpk/FGR5GLI6VJ4k88Lp8o+e/X9+YJUnCA9MLkwMIHfR8SgJFUWnFnN9TA=="
	kServerDomain        = "http://localhost:8081"
	AppPublicCertPath    = "E:\\goShopping\\superShopping\\Cert\\appCertPublicKey_2021000121669356.crt"         // app公钥证书路径
	AliPayRootCertPath   = "E:\\goShopping\\superShopping\\Cert\\alipayRootCert.crt"           // alipay根证书路径
	AliPayPublicCertPath = "E:\\goShopping\\superShopping\\Cert\\alipayCertPublicKey_RSA2.crt" // alipay公钥证书路径
	NotifyURL            = kServerDomain + "/#/login"
	ReturnURL            = kServerDomain + "/#/home"
	IsProduction         = false
	Timeout_express      = "60m"
)
var AliPayClient *Pay.AliPayClient

//重定向到支付宝二维码
func PayUrl(c *gin.Context) {
	OrderPrice, _ :=strconv.ParseFloat(c.Query("OrderPrice"),32)
	time1:=time.Now().String()
	time2:=fmt.Sprintf("%sOrder",time1)
	url, err := AliPayClient.Pay(Pay.Order{
		Timeout_express: Timeout_express,
		OutTradeNo:      time2,
		Subject:         "ttms购票" ,
		OrderPrice: float32(OrderPrice),
		Code:            Pay.LaptopWebPay,
	})
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusOK, "系统错误")
		return
	}
	order:=&Pay.Order{
		Subject:"ttms购票:",
		Timeout_express:Timeout_express,
		OutTradeNo:time2,
		OrderPrice: float32(OrderPrice),
		OrderStatus: 0,
		Tid:1,
		Uid:1,
		OrderCreateTime:time1,

	}
	err=Pay.CreateOrder(order)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusOK, err)
		return
	}
	c.Redirect(http.StatusTemporaryRedirect, url)
}

//支付后页面的重定向界面
func Callback(c *gin.Context) {
	_ = c.Request.ParseForm() // 解析form
	orderID, err := AliPayClient.VerifyForm(c.Request.Form)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusOK, "校验失败")
		return
	}
	c.JSON(http.StatusOK, "支付成功:"+orderID)
}

//支付成功后支付宝异步通知
func Notify(c *gin.Context) {
	_ = c.Request.ParseForm() // 解析form
	orderID, err := AliPayClient.VerifyForm(c.Request.Form)
	if err != nil {
		log.Println(err)
		return
	}
	log.Println("支付成功:" + orderID)
	// 做自己的事
}

